/*	 Lex Cube 4x4x4 can play snake 3D
**	 Tran Dai Hiep
**	 email: trandaihiep90@gmail.com
**
**
**
*/
#include <stdlib.h>
#include "TIMER\timer.h"
#include "LED\led.h"
#include "KEY\key.h"
#include "APP\app.h"

 /*********** Control Leb cube **********/
/* 	Use P0 and P1 for  Column- Row ,P2 for Floor   (Timer 1)
	P3 for Scan key	   (Timer 0)
	Scan LED (Timer 1)
	 */
#define sizeFloor 4

/*define for key*/
#define _NULL_ 	0xff
#define keyFinish 		12
#define keyExit 		10
#define keyA 			13
#define keyB 			16
#define keyC 			19
#define keyD 			22

/** define for application **/







#define TESTLEDKEY 		7		   

/* variable key */
unsigned char key;
/* variable led */
unsigned char dataP0[sizeFloor];
unsigned char dataP1[sizeFloor];
//unsigned char flag_1s = 0;
unsigned char flag_control = 0;
unsigned char flag = 0;
/*variable timer*/
unsigned int  counter0;

/* variable App*/
unsigned char snake_part[64];
unsigned char sizeSnake;
unsigned char xyzVictim;
unsigned char stateMove;
unsigned char xtemp=0xff;
unsigned char ytemp=0xff;
unsigned char ztemp=0xff;
/* variable used only main */
unsigned char i;
unsigned char check;
unsigned char keyStore;

void init_main()
{
	P0 = 0xff;
	P1 = 0x9f;
	P2 = 0xf0;
	P3 = 0xFF;
	IE = 0x82;
}

void setWaitOn(int TiMe){
	flag=0;
	counter0=TiMe;
}

void main()
{	
	init_main();
	init_timer0();
	init_timer1();
	clearLedAll();
	//TR1=0; //stop timer 1
	//TR0=0; //stop timer 0

	while(1) {
//		setbit(0,0,0);
//		setbit(1,1,1);
//		setbit(2,1,1);
//		setbit(1,2,2);
//		setfloor(2);
		setbit(2,2,2);
		setbit(2,2,2);
//		setrow(2,2);
//		setcolumn(1,1);
//	   srand(time());
	   	
		if (key!=_NULL_){
		   /****************Choose App**************/
		   /* 	Input any key to choose App
			*	Press OK to Submit
			*
			*	List app:
			*	Key 1: Led river
			*	Key 2: Snake
			*	Key 3: ...
			*
			*/
//			flag=1;
			clearLedAll();
			do{
				
				if (key!=_NULL_&&key!=keyFinish){
			 		keyStore=key; //Store key
			 		setValue_ledBonus(keyStore);
					//setbit(rand()%4 ,rand()%4 ,rand()%4);
			 	}
				i=rand();
			 	i=2000;			 	
				while(i>0&&key!=_NULL_)  // Wait new key
			 		i--;
			}while(key!=keyFinish);

//			flag=0;
			P2|=0x80;// On led

			// Switch to App
			switch(keyStore){
				case 0:
					//Call APP here
					break;
			 	case 1:
					//Call APP here
					break;
				case 2:
					//Call APP here
					break;
				case 3:
					//Call APP here
					break;
				case 4:
					//Call APP here
					break;
				case 5:
					do{
						check=1;
						for(i=0;i<4;i++){
						if (dataP0[i]!=0&&dataP0[i]!=0)
							check=0;
							break;
						}
						if(check==1)
							clearLedAll();
						while(setbit(rand()%4 ,rand()%4 ,rand()%4)==0);

						setWaitOn(100);
						while(flag==0){
							if(key==keyExit)
							break;
						}

					}while(key!=keyExit);
					break;
				case 6:		// Snake don't finish
/*****************************************************************/

				clearLedAll();
					initSnake();
					key=_NULL_;
					clearLedAll();
					i=0;
					while(i<64&&snake_part[i]!=0){//Display snake
							xtemp=(snake_part[i]%64)/16;
							ytemp=(snake_part[i]%16)/4;
							ztemp=snake_part[i]%4;
							setbit(xtemp,ytemp,ztemp);
							i++;
					}
					flag_control=2;// flash Victim
					while(key==_NULL_);//Pause. Wait any key 
					
					do{	
						clearLedAll();
						i=0;
						while(i<64&&snake_part[i]!=0){//Display snake
							xtemp=(snake_part[i]%64)/16;
							ytemp=(snake_part[i]%16)/4;
							ztemp=snake_part[i]%4;
							setbit(xtemp,ytemp,ztemp);
							i++;
						}
						setWaitOn(70);// Speed=1/50ms
						while(flag==0){
							if (key!=_NULL_){
				 				keyStore=key; //Store key
				 			}
						}
						check=0;
						switch(keyStore){
							case 1:
								stateMove=UP;
								break;
							case 2:
								stateMove=AHEAD;
								break;
							case 3:
								stateMove=UP;
								break;
							case 4:
								stateMove=LEFT;
								break;
							case 6:
								stateMove=RIGHT;
								break;
							case 7:
								stateMove=DOWN;
								break;
							case 8:
								stateMove=BACK;
								break;
							case 9:
								stateMove=DOWN;
								break;
							case keyA:// PAUSE
								check=1;
								break;
								
						}
						key=_NULL_;
						while(check==1&&key==_NULL_);//Pause. Wait any key 
						moveSnake();
					}while(keyStore!=keyExit);
/*****************************************************************/
					break;
				case TESTLEDKEY:   // Test key
/*****************************************************************/
					clearLedAll();
					do{
						if (key!=_NULL_&&key<9){
							keyStore=key%4;
							key=_NULL_;
							i=(i+1)%3;
							clearLedAll();
							switch(i){
							 	case 0:
									ztemp = keyStore;
									setfloor(ztemp);
									break;
							 	case 1:
									ytemp = keyStore;
									setrow(ytemp,ztemp);
									break;
							 	case 2:
									xtemp = keyStore;
									setbit(xtemp,ytemp,ztemp);
									break;

							}
			 			}

					}while(key!=keyExit);
/*****************************************************************/
					break;
			}
		clearLedAll();
		i=0;
			while(i<64&&snake_part[i]!=0){//Display snake
				snake_part[i]=0;
				i++;
			}
		}else{
			 // Process without key
		}
		
	}
}	
												